package pr.iceworld.fernando.seller.web;

import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import pr.iceworld.fernando.seller.service.UserService;
import pr.iceworld.fernando.seller.web.vo.LoginUserVO;

import javax.servlet.http.HttpServletRequest;

import static org.springframework.web.servlet.i18n.SessionLocaleResolver.LOCALE_SESSION_ATTRIBUTE_NAME;

@RestController
@Slf4j
public class LoginController {

    @Autowired
    UserService userService;

    @RequestMapping(value="/login", method = RequestMethod.GET)
    public String login(HttpServletRequest request) {
        log.info("session locale " + request.getSession().getAttribute(LOCALE_SESSION_ATTRIBUTE_NAME));
        return "login";
    }

    @RequestMapping(value = "/loginUser", method = RequestMethod.POST)
    public String login(HttpServletRequest request, Model model, LoginUserVO loginUserVO) throws Exception {

        return null;
    }

    @RequestMapping(value = "/index", method = RequestMethod.GET)
    public String index(Model model) {

        return "index";
    }


    @RequestMapping(value = "/logout", method = RequestMethod.GET)
    public String logout() {
        log.info("------------------------ logout start");
        Subject subject = SecurityUtils.getSubject();
        if (subject.isAuthenticated()) {
            subject.logout();
        }
        log.info("------------------------ logout end");
        return "login";
    }


}
